/**
 * 坐标系统测试脚本
 * 用于验证坐标转换是否与原始代码一致
 */

export default class CoordinateTester {
  constructor(junqiGame) {
    this.game = junqiGame;
  }

  // 测试坐标转换的一致性
  testCoordinateConversion() {
    console.log('=== 坐标转换测试 ===');

    // 测试几个关键位置
    const testPositions = [
      { x: 0, y: 0, isCircle: false },
      { x: 1, y: 2, isCircle: true },
      { x: 2, y: 5, isCircle: false },
      { x: 3, y: 7, isCircle: true },
      { x: 4, y: 11, isCircle: false }
    ];

    testPositions.forEach(pos => {
      const canvasPos = this.game.toCanvas(pos.x, pos.y, pos.isCircle);
      const gamePos = this.game.toGameCoords(canvasPos.x, canvasPos.y);

      console.log(`游戏坐标(${pos.x},${pos.y}) [${pos.isCircle ? '圆形' : '方形'}] -> Canvas坐标(${canvasPos.x},${canvasPos.y}) -> 回转游戏坐标(${gamePos.x},${gamePos.y})`);

      // 验证坐标转换是否正确（允许1个像素的误差）
      const xCorrect = Math.abs(gamePos.x - pos.x) <= 1;
      const yCorrect = Math.abs(gamePos.y - pos.y) <= 1;
      console.log(`  ${xCorrect && yCorrect ? '✓ 正确' : '✗ 错误'}`);
    });
  }

  // 测试大本营位置
  testBaseCamps() {
    console.log('\n=== 大本营位置测试 ===');

    Object.entries(this.game.baseCamps).forEach(([playerId, positions]) => {
      console.log(`${playerId === '0' ? '红方' : '黑方'}大本营:`);
      positions.forEach(([x, y]) => {
        const isCircle = this.game.list_circle.some(pos => pos[0] === x && pos[1] === y);
        const canvasPos = this.game.toCanvas(x, y, isCircle);
        console.log(`  游戏坐标(${x},${y}) [${isCircle ? '圆形' : '方形'}] -> Canvas坐标(${Math.round(canvasPos.x)},${Math.round(canvasPos.y)})`);
      });
    });
  }

  // 测试圆形位置
  testCirclePositions() {
    console.log('\n=== 圆形位置测试 ===');

    this.game.list_circle.forEach(([x, y]) => {
      const canvasPos = this.game.toCanvas(x, y, true);
      const expectedX = 120 * x + 30;
      const expectedY = 90 * y + 30;
      const actualX = (canvasPos.x - this.game.offsetX) / this.game.scale;
      const actualY = (canvasPos.y - this.game.offsetY) / this.game.scale;

      console.log(`游戏坐标(${x},${y}) -> 预期Canvas坐标(${expectedX},${expectedY}) -> 实际Canvas坐标(${Math.round(actualX)},${Math.round(actualY)})`);

      const xCorrect = Math.abs(actualX - expectedX) <= 1;
      const yCorrect = Math.abs(actualY - expectedY) <= 1;
      console.log(`  ${xCorrect && yCorrect ? '✓ 正确' : '✗ 错误'}`);
    });
  }

  // 测试方形位置
  testBlockPositions() {
    console.log('\n=== 方形位置测试 ===');

    // 测试几个方形位置
    const testBlocks = [
      [0, 0], [2, 2], [4, 4], [1, 6], [3, 9]
    ];

    testBlocks.forEach(([x, y]) => {
      const canvasPos = this.game.toCanvas(x, y, false);
      const expectedX = (2 * x) * 60;
      const expectedY = (3 * y) * 30;
      const actualX = (canvasPos.x - this.game.offsetX) / this.game.scale;
      const actualY = (canvasPos.y - this.game.offsetY) / this.game.scale;

      console.log(`游戏坐标(${x},${y}) -> 预期Canvas坐标(${expectedX},${expectedY}) -> 实际Canvas坐标(${Math.round(actualX)},${Math.round(actualY)})`);

      const xCorrect = Math.abs(actualX - expectedX) <= 1;
      const yCorrect = Math.abs(actualY - expectedY) <= 1;
      console.log(`  ${xCorrect && yCorrect ? '✓ 正确' : '✗ 错误'}`);
    });
  }

  // 运行所有测试
  runAllTests() {
    console.log('开始坐标系统测试...\n');

    this.testCoordinateConversion();
    this.testBaseCamps();
    this.testCirclePositions();
    this.testBlockPositions();

    console.log('\n坐标系统测试完成！');
  }
}